package com.controller;

import com.dto.BaseDto;
import com.entity.Comment;
import com.page.PageInfo;
import com.service.CommentService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

@Controller
@RequestMapping
//controller 配置Request mapping 使得放的热Requestmapping变短
public class CommentController {
    @Autowired
    private CommentService commentService;


    public CommentService getCommentService() {
        return commentService;
    }

    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    /**
     * @param pid 父id 常量的类别id
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @RequiresRoles("tt")
    @RequestMapping("comment/getCommentByPid/{pid}")
    @ResponseBody
    public BaseDto getCommentByPid(@PathVariable("pid") int pid) throws SQLException, ClassNotFoundException {
        String cacheId = "commentByPid"+pid;
        return commentService.getCommentByPid(pid,cacheId);
    }



    /**
     * 增加一个常量
     *
     * @param comment 常量对象
     * @return 返回插入的状态值 数字 为1成功 0失败 -值异常
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws SQLException             抛出sql异常
     */
    @RequestMapping("comment/add/{pid}")
    @ResponseBody
    public BaseDto addComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException{
        return commentService.addComment(comment);
    };

    /**
     * 修改常量信息
     *
     * @param comment
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @RequestMapping("comment/updateComment/{pid}")
    @ResponseBody
    public BaseDto updateComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException{
        return commentService.updateComment(comment);
    };

    /**
     * 根据主键删除
     *
     * @param coid
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @RequestMapping("comment/deleteComment/{pid}")
    @ResponseBody
    public BaseDto deleteComment(@PathVariable("pid") int coid) throws IllegalArgumentException, IllegalAccessException, SQLException{
        return commentService.deleteComment(coid);
    }
    /**
     * 根据常量分类 分页查询，分页查询 就是 mysql的limit 语句需要2个参数 参数1从几开始取 参数2 取多长
     * @param pageInfo
     * @return
     * @throws Exception
     */
    @RequestMapping("comment/queryCommentsByPage")
    @ResponseBody
    public BaseDto queryCommentsByPage(PageInfo pageInfo, int pid) throws Exception {
        System.out.println(pid);
        System.out.println("-------------"+pageInfo.getCurrPage() +" " +pageInfo.getPageSize()+"------------------------------");
        return commentService.queryCommentsByPage(pageInfo,pid);
    }
}
